package com.jnc.web.biz.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.jnc.common.constant.RedisConstant;
import com.jnc.common.constant.SysConstant;
import com.jnc.common.util.convert.ConvertUtil;
import com.jnc.core.config.redis.RedisStore;
import com.jnc.web.biz.system.mapper.DictDataMapper;
import com.jnc.web.biz.system.model.DictData;
import com.jnc.web.biz.system.service.DictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: jjn
 * @Date: 2018/11/7
 * @Description:
 */
@Service
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private RedisStore redisStore;

    @Override
    public List<DictData> queryDictData(DictData record) {
        return dictDataMapper.queryDictData(record);
    }

    @Override
    public DictData getDictData(Integer dictDataId) {
        return dictDataMapper.getDictData(dictDataId);
    }

    @Override
    public List<DictData> getDictDataByType(String dictType) {
        return dictDataMapper.getDictDataByType(dictType);
    }

    @Override
    public DictData getDictDataValue(DictData record) {
        return dictDataMapper.getDictDataValue(record);
    }

    @Override
    public List<DictData> checkDictData(DictData record) {
        return dictDataMapper.checkDictData(record);
    }

    @Transactional(value = SysConstant.TX_BASE_NAME)
    @Override
    public int deleteDictData(String ids) {
        List<Integer> list = ConvertUtil.toIntList(ids);
        return dictDataMapper.deleteDictData(list);
    }

    @Transactional(value = SysConstant.TX_BASE_NAME)
    @Override
    public int addDictData(DictData record) {
        return dictDataMapper.addDictData(record);
    }

    @Transactional(value = SysConstant.TX_BASE_NAME)
    @Override
    public int editDictData(DictData record) {
        return dictDataMapper.editDictData(record);
    }

    /**
     * 根据字典类型、字典编码，获取字典值(先从缓存取，缓存没有再从数据库取。服务端项目已加载到redis)
     * @param dictType
     * @param dictCode
     * @return
     */
    @Override
    public String getValueBy(String dictType, String dictCode) {
        String key = RedisConstant.DICT_DATA + dictType;
        String hashValues = (String) redisStore.getHashValues(key, dictCode);
        //为空，则从数据库里取，并设置到redis
        if(StringUtils.isBlank(hashValues)){
            boolean flag = redisStore.exists(key);
            if(flag){
                String value = null;
                DictData model = new DictData();
                model.setDictType(dictType);
                model.setDictCode(dictCode);
                DictData temp = dictDataMapper.getDictDataValue(model);
                if(temp != null){
                    redisStore.putHashValue(key, dictCode, temp.getDictValue());
                }
            }else{
                List<DictData> list = dictDataMapper.getDictDataByType(dictType);
                if(CollectionUtil.isEmpty(list)){
                    return hashValues;
                }
                Map<String, Object> map = MapUtil.newHashMap();
                for (DictData obj : list){
                    map.put(obj.getDictCode(), obj.getDictValue());
                    if(dictCode.equals(obj.getDictCode())){
                        hashValues = obj.getDictValue();
                    }
                }
                //先清空老的缓存数据
                redisStore.del(key);
                //再存入新的缓存数据
                redisStore.putHashValues(key, map);
            }
        }
        return hashValues;
    }
}
